import { Component, OnInit } from '@angular/core';
import { PaysetService } from '../../service/payset.service';
import { UserService } from '../../service/user.service';
import { MerchantService } from '../../service/merchant.service';
import { NzMessageService } from 'ng-zorro-antd';
import { CommonService } from 'src/app/service/common.service';
import { ComponentBasic } from '../../app.component';
import { ActivatedRoute } from '@angular/router';
@Component({
  selector: 'app-payset',
  templateUrl: './payset.component.html',
  styleUrls: ['./payset.component.scss'],
  providers: [PaysetService, CommonService,UserService,MerchantService]
})
export class PaysetComponent extends ComponentBasic implements OnInit {

  loading = true;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  dataSet = [];
  isVisible = false;
  isinfoVisible = false;
  isOkLoading = false;
  roleItems: Array<any> = []
  doc: any = {};
  remarks: any;
  certificate: any;
  sdate: any;
  edate: any;
  iscontact: any;
  merid:any;
  statuslist = [{ key: null, value: '全部' }, { key: '1', value: '已联系' }, { key: '0', value: '未联系' }];
  constructor(private merchantservice:MerchantService,private userservice:UserService,private paysetService: PaysetService, private msg: NzMessageService, private commonservice: CommonService, private ar: ActivatedRoute) {
    super();
    super.setLayout(true, true);
    this.merid = this.ar.snapshot.params.id;
  }

  ngOnInit() {
    
    this.searchData();
  }
  searchData(reset: boolean = false): void {
    this.loading = true;
    this.paysetService.query({
      merid: this.merid,
    }).then((res) => {
      this.loading = false;
      //this.total = res.total;
      //console.log(res);
      this.dataSet = res;
    });
  }
  //编辑
  Edit(id: any) {
    this.isinfoVisible = true;
    this.paysetService.querybyId({ _id: id }).then((res) => {
      this.doc = res;
    });
  }
  Delete(id:any){

    if(confirm("确定删除?")){
      this.paysetService.logicdelete({ _id: id }).then((res) => {
        this.searchData();
      });
    }
   
  }
  Add(){
    this.isinfoVisible = true;
    this.merchantservice.queryById(this.merid).then((res) => {
      this.doc.merchantname=res.name;
    });
  }
  //根据用户手机号搜索用户
serchuser(){
   this.userservice.getuserinfobyphone({phone:this.doc.wechatphone}).then((res) => {
     if(res){
      this.doc.wechatopenid = res.openid;
     }else{
      this.msg.error("不存在该小程序用户");
     }
   
  });;
}
  //确定修改
  handleOk(): void {
    this.isOkLoading = true;
    if(!this.doc.wechatphone){
      this.msg.error("请输入手机号");
      return 
    }

    if(!this.doc.wechatrealname){
      this.msg.error("请输入真实姓名");
      return 
    }

    if(!this.doc.wechatidnum||this.doc.wechatidnum.length<18){
      this.msg.error("请输入身份证号并且不少于18位");
      return 
    }

    if(!this.doc.wechatopenid){
      this.msg.error("获取openid");
      return 
    }

    if(!this.merid){
      this.msg.error("url错误");
      return 
    }

    this.doc.merchantid= this.merid;
    this.paysetService.save(this.doc).then((res) => {
      this.searchData();
      this.isinfoVisible = false;
      this.doc = {};
    });

  }

  handleCancel(): void {
    this.isinfoVisible = false;
    this.doc = {};
  }

  bhandleCancel(): void {
    this.isVisible = false;
  }

  bhandleOk(): void {
    this.isVisible = false;
  }

 

}
